package com.hyjx.framework.component.log;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日志控制器
 */
@Controller
@RequestMapping("/logbusiness")
public class LogBusinessController {
    @Resource
    private LogBusiness service;

    /**
     * 进入到信息查询页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoList", method = RequestMethod.GET)
    public String gotoList(HttpServletRequest request) {
        return "/framework/log/list";
    }

    /**
     * 执行客户查询操作
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String queryList(LogBusinessModel model, HttpServletRequest request) {
        return service.queryList(model);
    }

    /**
     * 信息查看
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoView", method = RequestMethod.GET)
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        LogBusinessModel model = service.getInfoById(id);
        LogBusinessDataModel ldm = new LogBusinessDataModel();
        ldm.setLbid(id);
        List<LogBusinessDataModel> listdata = service.getLogDataModel(ldm);
        ModelAndView mav = new ModelAndView("/framework/log/view");
        mav.addObject("model", model);
        mav.addObject("logdata", listdata);
        return mav;
    }

    @RequestMapping(value = "/fwtj", method = RequestMethod.GET)
    @ResponseBody
    public String fwtj(LogBusinessModel model, HttpServletRequest request) {
        return service.queryfwtj(model);
    }

    /**
     * 执行删除一个月前的数据
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteMonthData", method = RequestMethod.POST)
    @ResponseBody
    public String deleteMonthData(HttpServletRequest request) {
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -30);  //设置为30天
        dBefore = calendar.getTime();   //得到前30天的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置时间格式
        String defaultStartDate = sdf.format(dBefore);   //格式化前30天
        String defaultEndDate = sdf.format(dNow); //格式化当前时间
        String rs = service.deleteMonthData(defaultStartDate);

        return rs;
    }

    @RequestMapping(value = "/gotoDataList", method = RequestMethod.GET)
    public String gotoDataList(HttpServletRequest request) {
        return "/framework/log/datalist";
    }

    /**
     * 执行客户查询操作
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryDataList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String gotoDataList(LogBusinessDataModel model, HttpServletRequest request) {
        return service.queryDataList(model);
    }


}